/*
 * Copyright (c) 2009 Nathaniel Houghton <nathan@brainwerk.org>
 *                    Robert Kirchgessner <rkirchge@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose with or without fee is hereby granted, provided that
 * the above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>

#define WINDOWS
#include "elm.h" 
#include "obd_pid.h"
#include "elm_dtc.h"

uint8_t 
btoh(uint8_t *bytes, char *buf, uint8_t len)
{
	uint8_t state;
	uint8_t i;
	uint8_t index;
	uint8_t c;
	
	state=0;
	index=0;
	i=0;
	
	while((c=buf[i++]) != '\0') {
		
		if( index == len )/*Incase the buffer gets full.*/
			break;
		
		if( isspace(c) )
			continue;

		if( c<='F' && c>='A' ) /*Check if we get a character or not*/
			c = c-'A'+10;
		else
			c = c-'0';

		if(state == 0) { /*Upper Nisbble*/
			bytes[index] = c<<4;
			state=1;
		}
		else {		/*Lower Nibble*/		
			bytes[index++] += c;
			state=0;
		}
	}
	
	return 0;
}

int main(int argc, char* argv[])
{
	char buf[] = "0014";
	uint8_t this[12];
	
	btoh(this, buf, sizeof(this));
	
	printf("Little Indian* %04x\n", this[0]<<8 | this[1]);
	printf("Big Indian* %04x\n", this[1]<<8 | this[0]);
/*
	printf("Test\n");
	
	uint8_t resp;
	int i;
	
	while(1) {
	resp = elm_dtc_peek();
	
	elm_dtc_update( resp );
	
	for(i=0; i< (resp&0x7F); ++i)
		printf("%x\n", elm_dtc_next() );
	
	for(i=0; i< (resp&0x7F); ++i)
		printf("%x\n", elm_dtc_prev() );
	
	}
	*/
	return 0;
}




